package com.example.njupt.uis.mine;

import android.os.Build;
import android.widget.LinearLayout;

import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.njupt.R;
import com.example.njupt.base.fragment.BaseFragment;
import com.example.njupt.constant.ARouteConstance;
import com.example.njupt.helper.ARouteUtils;
import com.example.njupt.helper.DimenUtils;
import com.example.njupt.helper.RecycleViewDivider;
import com.example.njupt.okhttp.entity.MineListEntity;
import com.example.njupt.okhttp.retrofit.BaseResult;
import com.google.android.material.appbar.AppBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterActivityLaunchConfigs;

/**
 * Author:      mwang
 * Create Date: Created in 2021/5/25 17:49
 * Update Date:
 * Modified By:
 * Description:
 */
public class MineFragment extends BaseFragment<MinePresenter> {

    @BindView(R.id.app_bar)
    AppBarLayout appBarLayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.ll_head)
    LinearLayout llHead;

    private List<MineListEntity> mineList = new ArrayList<>();

    @Override
    public void onLazyLoad() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        initToolbar();
        final int alphaMaxOffset = DimenUtils.dip2px(getContext(), 220);
        toolbar.getBackground().setAlpha(0);
        appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
            // 设置 toolbar 背景
            if (verticalOffset > -alphaMaxOffset) {
                toolbar.getBackground().setAlpha(255 * -verticalOffset / (alphaMaxOffset * 2));
            } else {
                toolbar.getBackground().setAlpha(255);
            }
        });
        appBarLayout.setExpanded(true);
        initList();
        initAdapter();
        recyclerView.stopScroll();
        recyclerView.scrollToPosition(0);
        RecycleViewDivider divider = new RecycleViewDivider(
                getContext(), LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(getContext(), R.color.main_line));
        recyclerView.addItemDecoration(divider);
    }

    private void initToolbar() {
        try {
            if (toolbar != null) {
                int statusBarHeight = getStatusBarHeight(getContext());
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    toolbar.setPadding(0, statusBarHeight, 0, 0);
                    toolbar.getLayoutParams().height = DimenUtils.dip2px(getContext(), 50) + statusBarHeight;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initList() {
        mineList.clear();
        MineListEntity entity = new MineListEntity();
        entity.setString("物理学根本就不存在");
        for (int i = 0; i < 30; i++) {
            mineList.add(entity);
        }
    }

    private void initAdapter() {
        MineListAdapter adapter = new MineListAdapter(mineList);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        adapter.setRecyclerView(recyclerView);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter1, view, position) -> startActivity(
                FlutterActivity
                        .withCachedEngine("njupt_flutter_engine")
                        .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
                        .build(getContext())));
    }

    @Override
    protected void initListener() {
        llHead.setOnClickListener(v -> ARouteUtils.routerWithAnimation(ARouteConstance.ACTIVITY_MVP).navigation());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected MinePresenter createPresenter() {
        return null;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void onErrorCode(BaseResult result) {

    }
}
